package com.lycx.businessmanager.service.impl;

import java.util.Date;
import java.util.List;

import com.lycx.businessmanager.constant.ConstantsDbOperation;
import com.lycx.businessmanager.constant.ConstantsMessage;
import com.lycx.businessmanager.constant.ConstantsTableName;
import com.lycx.businessmanager.dao.DeptDao;
import com.lycx.businessmanager.manager.SequenceManager;
import com.lycx.businessmanager.model.Dept;
import com.lycx.businessmanager.service.IDeptService;

public class DeptServiceImpl implements IDeptService {

	private DeptDao deptDao;

	public void setDeptDao(DeptDao deptDao) {
		this.deptDao = deptDao;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Dept> select(Dept dept) {
		return (List<Dept>) deptDao.select(ConstantsDbOperation.DEPT_SELECT, dept);
	}

	@Override
	public String add(Dept dept) {
		String result = null;
		try {
			List<Dept> list = selectRepeat(dept);
			if (list != null && list.size() > 0) {
				result = ConstantsMessage.DEPT_REPEAT_FAIL;
			} else {
				dept.setId(SequenceManager.createId(ConstantsTableName.DEPT));
				dept.setCreateTime(new Date());
				deptDao.insert(ConstantsDbOperation.DEPT_INSERT, dept);
				result = ConstantsMessage.DEPT_ADD_SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = ConstantsMessage.DEPT_ADD_FAIL;
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	private List<Dept> selectRepeat(Dept dept) {
		return (List<Dept>) deptDao.select(ConstantsDbOperation.DEPT_SELECT_REPEAT, dept);
	}

	@Override
	public String del(int[] ids) {
		String result = null;
		try {
			if (ids != null && ids.length > 0) {
				for (int id : ids) {
					Dept dept = new Dept();
					dept.setId(id);
					deptDao.delete(ConstantsDbOperation.DEPT_DELETE, dept);
					deleteChildren(dept);
				}
				result = ConstantsMessage.DEPT_DEL_SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = ConstantsMessage.DEPT_DEL_FAIL;
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	private void deleteChildren(Dept dept) {
		List<Dept> deptList = deptDao.select(ConstantsDbOperation.DEPT_SELECT_CHILDREN, dept);
		if(deptList != null && deptList.size() > 0){
			for(Dept deptTemp : deptList){
				deleteChildren(deptTemp);
				deptDao.delete(ConstantsDbOperation.DEPT_DELETE, deptTemp);
			}
		}
	}

	@Override
	public String mod(Dept dept) {
		String result = null;
		try{
			List<Dept> list = selectRepeat(dept);
			if (list != null && list.size() > 0) {
				result = ConstantsMessage.DEPT_REPEAT_FAIL;
			} else {
		        deptDao.update(ConstantsDbOperation.DEPT_UPDATE, dept);
		        result = ConstantsMessage.DEPT_MOD_SUCCESS;
			}
		    
		}catch(Exception e){
			e.printStackTrace();
			result = ConstantsMessage.DEPT_MOD_FAIL;
		}
        return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Dept> selectForTree(Dept dept) {
		List<Dept> deptList = deptDao.select(ConstantsDbOperation.DEPT_SELECT_CHILDREN, dept);
		if(deptList != null && deptList.size() > 0){
			for(Dept deptTemp : deptList){
				selectChildren(deptTemp);
			}
		}
		return deptList;
	}

	@SuppressWarnings("unchecked")
	private void selectChildren(Dept dept) {
		List<Dept> deptList = deptDao.select(ConstantsDbOperation.DEPT_SELECT_CHILDREN, dept);
		if(deptList != null && deptList.size() > 0){
			for(Dept deptTemp : deptList){
				selectChildren(deptTemp);
			}
			dept.setChildren(deptList);
		}
	}
	
	
}
